#!/bin/bash

#
# cat result.txt | grep 人口 | sed 's/万人.*//g' | awk -F : '{print $2}'
# cat result.txt | grep 总值 | sed 's/亿元.*//g' | awk -F : '{print $2}'
# cat result.txt | grep 收入 | sed 's/亿元.*//g' | awk -F : '{print $2}'
# cat result.txt | grep 支出 | sed 's/亿元.*//g' | awk -F : '{print $2}'
#

items=("城市" "人口" "总值" "收入" "支出")
units=("年份" "万人" "亿元" "亿元" "亿元")

citys=("china" "shanghai" "beijing" "shenzhen" "guangzhou" "chongqing" "suzhou" "chengdu" "hangzhou" "wuhan" "nanjing" "tianjin" "ningbo" "changsha" "foshan" "jinan" "fuzhou" "xian" "xiamen" "ganzhou")
names=("全国" "上海" "北京" "深圳" "广州(doc)" "重庆" "苏州" "成都(pdf)" "杭州" "武汉" "南京" "天津" "宁波" "长沙" "佛山(pdf)" "济南" "福州" "西安(pdf)" "厦门" "赣州")

year=$1

if [ -z $year ];then
	echo "usage: $0 <year>"
	echo "example: $0 2021"
	exit 1
fi

head=
line=
result_file=result_${year}.txt

for i in `seq 0 ${#items[*]}`;
do
	#echo $i

	if [ $i -eq ${#items[*]} ];then
		printf "\n"
		break;
	fi

	head=$head"${items[$i]}/${units[$i]}\t"
	printf "%s/%s\t" ${items[$i]} ${units[$i]}
done

#echo -e $head
#echo -e $head > $result_file

for i in `seq 0 ${#citys[*]}`;
do
	if [ $i -eq ${#citys[*]} ];then
		break;
	fi

	#result=`./${citys[$i]}.sh $year`
	./${citys[$i]}.sh $year > result.txt
	result=result.txt

	#echo "======= ${names[$i]} ======="
	#echo -e $result
	#ren=`echo $result | grep 人口 | sed 's/万人.*//g' | awk -F : '{print $2}'`
	#gdp=`echo $result | grep 总值 | sed 's/亿元.*//g' | awk -F : '{print $2}'`
	#in=`echo $result | grep 收入 | sed 's/亿元.*//g' | awk -F : '{print $2}'`
	#out=`echo $result | grep 支出 | sed 's/亿元.*//g' | awk -F : '{print $2}'`

	ren=`cat $result | grep 人口 | sed 's/万人.*//g' | awk -F : '{print $2}' | grep -o '[0-9.]\+'`
	gdp=`cat $result | grep 总值 | sed 's/亿元.*//g' | awk -F : '{print $2}' | grep -o '[0-9.]\+'`
	in=`cat $result | grep 收入 | sed 's/亿元.*//g' | awk -F : '{print $2}'  | grep -o '[0-9.]\+'`
	out=`cat $result | grep 支出 | sed 's/亿元.*//g' | awk -F : '{print $2}' | grep -o '[0-9.]\+'`

	if [ -z $ren ];then
		ren=0
	fi

	#line="${names[$i]}\t$ren\t$gdp\t$in\t$out\t"
	#echo -e $line
	#echo -e $line > $result_file

	printf "%10s\t%10.2f\t%10.2f\t%10.2f\t%10.2f\t\n" ${names[$i]} $ren $gdp $in $out
done

exit 0

